package com.java.control.impl;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;

import com.java.common.DateUtil;
import com.java.common.SessionInfo;
import com.java.control.AbstractAdminFormController;
import com.java.entity.QIndex;
import com.java.entity.User;
import com.java.service.QuotationService;
import com.java.service.UserService;

/**
 * 产品添加
 * @author wqf
 * @version  Nov 23, 2011 1:10:42 PM
 */
public class QIndexEditController extends AbstractAdminFormController{

	public QIndexEditController(){
		this.setCommandClass(QIndex.class);
	}
		
	protected void addSubmit(HttpServletRequest request,HttpServletResponse response, Object command, BindException errors) throws Exception {
		QIndex qIndex = (QIndex)command;		
		
		this.quotationService.editQIndex(qIndex);
		request.setAttribute("operation", "add");
	}	
	
	protected void modifySubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception{		
		QIndex qIndex = (QIndex)command;
		boolean exist = this.quotationService.existsQIndexByQId(qIndex.getId(), qIndex.getQid());
		
		this.quotationService.editQIndex(qIndex);
		
		request.setAttribute("operation", "modify");
		request.setAttribute("id", qIndex.getId());
	}
		
	protected Object getCommandDataForModify(HttpServletRequest request){
		Long id=Long.valueOf(request.getParameter("id"));
		QIndex qIndex = this.quotationService.getQIndexById(id);		
		return qIndex;
	}
	
	protected Object getCommandDataForAdd(HttpServletRequest request) {
		QIndex qindex = new QIndex();
		qindex.setqDate(new Date());
		
		User user = this.userService.getUserById(getSessionInfo(request).getUserId());
		String qid="Q"+DateUtil.getDateTimeFormString("yyyyMMdd",new Date())+"-";
		if(user.getCode()!=null){
			qid = qid +(user.getCode().length()>2?user.getCode().substring(0, 2):user.getCode());
		}		
		String seri = this.quotationService.getQIndexMaxPid(qid);
		
		qindex.setQid(seri);
		return qindex;
	}
			
	public QuotationService quotationService;
	public UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public QuotationService getQuotationService() {
		return quotationService;
	}

	public void setQuotationService(QuotationService quotationService) {
		this.quotationService = quotationService;
	}

	
}
